<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="领料编码" prop="pickCode">
        <el-input v-model="queryParams.pickCode" placeholder="请输入领料编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="车间任务ID" prop="wkspTaskId">
        <el-input v-model="queryParams.wkspTaskId" placeholder="请输入车间任务ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="物料编码" prop="mtrCode">
        <el-input v-model="queryParams.mtrCode" placeholder="请输入物料编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="物料名称" prop="mtrName">
        <el-input v-model="queryParams.mtrName" placeholder="请输入物料名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="需领料数量" prop="pickQuantity">
        <el-input v-model="queryParams.pickQuantity" placeholder="请输入需领料数量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="出库状态" prop="ispicked">
        <el-select v-model="queryParams.ispicked" placeholder="请选择状态 " clearable>
          <el-option v-for="dict in dict.type.mes_outhou_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['mes:pick:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['mes:pick:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mes:pick:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['mes:pick:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pickList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="" align="center" prop="id" /> -->
      <el-table-column label="领料编码" align="center" prop="pickCode" />
      <el-table-column label="车间任务编码" align="center" prop="wkspCode" />
      <!-- <el-table-column label="物料编码" align="center" prop="mtrCode" /> -->
      <!-- <el-table-column label="物料名称" align="center" prop="mtrName" /> -->
      <!-- <el-table-column label="需领料数量" align="center" prop="pickQuantity" /> -->
      <el-table-column label="出库状态" align="center" prop="ispicked" >
        <template slot-scope="scope">
          <el-tag :type="tagType(scope.row.ispicked)" effect="dark">
            <dict-tag :options="dict.type.mes_outhou_status" :value="scope.row.ispicked" />
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.ispicked==0" size="mini" type="text" icon="el-icon-edit" @click="chukucheck(scope.row)"
            v-hasPermi="['mes:pick:edit']">原料出库</el-button>
          <el-button v-if="scope.row.ispicked==0" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['mes:pick:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />



    <el-dialog title="领料详情" :visible.sync="chukuopen" v-if="chukuopen" width="700px" append-to-body>
      <label style="margin-bottom: 10px;display: block;">领料编码:{{ pickListDetails.pickInfo.pickCode }}</label>

      <el-table :data="pickListDetails.mtrs" border style="width: 100%">
        <el-table-column prop="mtrName" label="物料名称">
        </el-table-column>
        <el-table-column prop="mtrCode" label="物料编码">
        </el-table-column>
        <el-table-column prop="mtrQuantity" label="领料数量">
        </el-table-column>
      </el-table>
      <el-divider>库存详情</el-divider>
      <el-table :data="pickListDetails.mtrStocks" border style="width: 100%">
        <el-table-column prop="mtrCode" label="物料编码">
        </el-table-column>
        <el-table-column prop="mtrName" label="物料名称">
        </el-table-column>
        <el-table-column prop="houName" label="库房名称">
        </el-table-column>
        <el-table-column prop="mtrQuantity" label="库存数量">
        </el-table-column>
        <el-table-column prop="locatorCode" label="库位编码">
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="pickListDetails.pickInfo.isdispatch == 0"  type="primary" @click="dispatchAvg()">派遣小车</el-button>
        <el-button v-else type="primary" @click="chuku()">确 认 领 料</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>




    <!-- 添加或修改领料信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="领料编码" prop="pickCode">
          <el-input v-model="form.pickCode" placeholder="请输入领料编码" />
        </el-form-item>
        <el-form-item label="车间任务ID" prop="wkspTaskId">
          <el-input v-model="form.wkspTaskId" placeholder="请输入车间任务ID" />
        </el-form-item>
        <el-form-item label="物料编码" prop="mtrCode">
          <el-input v-model="form.mtrCode" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料名称" prop="mtrName">
          <el-input v-model="form.mtrName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="需领料数量" prop="pickQuantity">
          <el-input v-model="form.pickQuantity" placeholder="请输入需领料数量" />
        </el-form-item>
        <el-form-item label="已领料" prop="ispicked">
          <el-input v-model="form.ispicked" placeholder="请输入已领料" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPick, getPick, delPick, addPick, updatePick, getDetails,outBound ,dispatchAvg} from "@/api/mes/pick";

export default {
  name: "Pick",
  dicts: ['mes_outhou_status'],
  data() {
    return {
      //当前点击记录id
      nowid:"",
      //领料详情数据
      pickListDetails: [],
      //原料出库对话框
      chukuopen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 领料信息表格数据
      pickList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pickCode: null,
        wkspTaskId: null,
        mtrCode: null,
        mtrName: null,
        pickQuantity: null,
        ispicked: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //派遣小车
    dispatchAvg(){
      dispatchAvg({id:this.pickListDetails.pickInfo.id}).then(res=>{
        this.$modal.msgSuccess(res.msg);
      }).then(()=>{
        this.chukucheck({id:this.nowid})
      })
        
    },
    //原料确认出库
    chuku(){
      outBound({id:this.nowid}).then(res=>{
        this.chukuopen = false
        this.getList()
        this.$modal.msgSuccess("领料成功");
      })
    },
    // 原料出库按钮
    chukucheck(row) {
      this.nowid=row.id
      
      getDetails({ id: row.id }).then(res => {
        console.log("🚀 ~ file: index.vue:166 ~ getDetails ~ res:", res)
        this.pickListDetails = res.data
        if (this.pickListDetails.error != null) {
          this.$modal.msgError(this.pickListDetails.error);
        }
        this.chukuopen = true
      })

    },
        //标签类型判断
        tagType(status) {
      switch (status) {
        case 0:
          return ""
        case 1:
          return "success"
      }
    },
    /** 查询领料信息列表 */
    getList() {
      this.loading = true;
      listPick(this.queryParams).then(response => {
        this.pickList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.chukuopen=false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pickCode: null,
        wkspTaskId: null,
        mtrCode: null,
        mtrName: null,
        pickQuantity: null,
        ispicked: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加领料信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPick(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改领料信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePick(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPick(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除领料信息编号为"' + ids + '"的数据项？').then(function () {
        return delPick(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/pick/export', {
        ...this.queryParams
      }, `pick_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
